package com.sky.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;



    /**
     * 根据菜品ID添加购物车
     * @param shoppingCartDTO
     */
    public void saveWithDishId(ShoppingCartDTO shoppingCartDTO) {
        Long dishId = shoppingCartDTO.getDishId();
        //1. 查询菜品相关信息
        Dish dish = dishMapper.getById(dishId);
        addOrUpdate(shoppingCartDTO,null,dish);
    }

    /**
     * 根据套餐ID添加购物车
     * @param shoppingCartDTO
     */
    public void saveWithSetMealId(ShoppingCartDTO shoppingCartDTO) {
        Long setmealId = shoppingCartDTO.getSetmealId();
        //1. 查询套餐相关信息
        Setmeal setmeal = setmealMapper.getById(setmealId);
        addOrUpdate(shoppingCartDTO,setmeal,null);
    }

    @Override
    public void save(ShoppingCartDTO shoppingCartDTO) {
        //1. 创建购物车对象
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtil.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //2. 查询购物车是否存在
        List<ShoppingCart> list=shoppingCartMapper.select(shoppingCart);
        if (list!=null&&list.size()>0){
            //2.1 存在，更新数据
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.updateNumberById(cart);
            return;
        }
        //2.2 不存在，插入一条数据

        //3. 判断点的是菜品还是套餐
        Long dishId = shoppingCartDTO.getDishId();
        String name;
        BigDecimal price;
        String image;
        if (dishId!=null){
            //3.1 查询菜品数据
            Dish dish = dishMapper.getById(dishId);
            name=dish.getName();
            price=dish.getPrice();
            image=dish.getImage();
        }else{
            //3.2 查询套餐数据
            Long setmealId = shoppingCartDTO.getSetmealId();
            Setmeal setmeal = setmealMapper.getById(setmealId);
            name=setmeal.getName();
            price=setmeal.getPrice();
            image=setmeal.getImage();
        }
        shoppingCart.setName(name);
        shoppingCart.setAmount(price);
        shoppingCart.setImage(image);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setNumber(1);

        //插入购物车数据
        shoppingCartMapper.insert(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> list() {
        //1. 根据当前用户id获得购物车数据
        Long currentId = BaseContext.getCurrentId();
        return shoppingCartMapper.getByUserId(currentId);
    }

    /**
     * 清空购物车
     */
    public void clean() {
        //1. 根据当前用户id清空购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(currentId);
    }

    /**
     * 删除购物车中的一个商品
     * @param shoppingCartDTO
     */
    public void delete(ShoppingCartDTO shoppingCartDTO) {
        //1. 新建一个购物车实体
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtil.copyProperties(shoppingCartDTO,shoppingCart);
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        //2. 判断商品是否只有一份
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);
        if (list!=null&&list.size()!=0){
            ShoppingCart cart = list.get(0);
            Integer number = cart.getNumber();
            if (number==1){
                //2.1 只有一份，直接删除
                shoppingCartMapper.delete(shoppingCart);
                return;
            }
            //2.2 有多份，更新数据
            cart.setNumber(cart.getNumber()-1);

            //3. 插入更新后的数据
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 新增或更新购物车
     * @param shoppingCartDTO
     * @param setmeal
     * @param dish
     */
    private void addOrUpdate(ShoppingCartDTO shoppingCartDTO, Setmeal setmeal,Dish dish){
        Long currentId = BaseContext.getCurrentId();
        String name;
        BigDecimal price;
        String image;
        if (setmeal==null){
            name=dish.getName();
            price=dish.getPrice();
            image=dish.getImage();
        }else {
            name=setmeal.getName();
            price=setmeal.getPrice();
            image=setmeal.getImage();
        }

        //2. 判断套餐是否已经在购物车中(根据用户id查询和套餐名称）
        ShoppingCart shoppingCart=shoppingCartMapper.getByUserIdAndName(currentId,name);

        //3. 判断是否购物车是否为空
        if (shoppingCart==null) {
            //3.1 插入一条购物车数据
            shoppingCart=ShoppingCart.builder()
                    .userId(currentId)
                    .number(1)
                    .amount(price)
                    .image(image)
                    .name(name)
                    .createTime(LocalDateTime.now())
                    .build();
            BeanUtil.copyProperties(shoppingCartDTO,shoppingCart);
            shoppingCartMapper.insert(shoppingCart);
        }
        //3.2 更新购物车数据
        shoppingCart.setAmount(price.add(shoppingCart.getAmount()));   //金额+单价price
        shoppingCart.setNumber(shoppingCart.getNumber()+1);  //数量+1
        shoppingCartMapper.update(shoppingCart);
    }
}
